package com.china08.yxyapi.api.manage.tec.teaching.lecture.pack;

import java.util.function.Predicate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResResource;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePack;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePack.Res;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResResourceRepository;
import com.china08.yxyapi.repository.mg.lecture.YxyLecturePackRepository;
import com.china08.yxyapi.security.profile.YxyProfile;
import com.china08.yxyapi.service.yxyresource.YxyResourceService;
import com.china08.yxyapi.util.DateToolUtils;
import com.china08.yxyapi.util.EsResourcePvUpdateUtil;
import com.china08.yxyapi.util.YxyPlayUrlForWeiKechengBuilder;
import com.china08.yxyapi.util.YxyResUtil;

/**
 * 老师 ---备授课--授课包资源详情
 * 
 */
@CrossOrigin
@RestController
public class LectureResGetPackController extends YxyApiBaseController {

    @Autowired
    private YxyLecturePackRepository yxyLecturePackRepository;

    @Autowired
    private YxyResourceService ysr;

    @Autowired
    private YxyResUtil yxyResUtil;

    @Autowired
    YxyPlayUrlForWeiKechengBuilder builder;

    @Autowired
    private YxyResResourceRepository yrrr;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/pack/{packId}/res/{resId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object process(HttpServletRequest request, HttpServletResponse response,
            @PathVariable(value = "packId") String packId, @PathVariable(value = "resId") String resId) {
        String userId = getProfile(request, response).getId();
        String schoolId = getProfile(request, response).getSchoolId();
        YxyProfile profile = getProfile(request, response);
        // ### 备课包
        YxyLecturePack lecturePack = yxyLecturePackRepository.findByIdAndUserIdAndSchoolId(packId, userId, schoolId);
        Assert.notNull(lecturePack, "授课包找不到了，请刷新页面");
        Assert.isTrue(lecturePack.getResources().size() > 0, "授课包出错了请刷新页面");
        EsResourcePvUpdateUtil.updateEsResoure(resId + "_1");
        // ### 判断资源是否存在于这个备课包
        Predicate<Res> p1 = e -> StringUtils.equals(e.getResourceId(), resId);
        boolean isok = lecturePack.getResources().stream().anyMatch(p1);
        Assert.isTrue(isok, "授课包出错了请刷新页面");
        // ### 资源查询
        YxyResResource elasticYxyResResource = yrrr.findById(resId);
        // ElasticYxyResResource elasticYxyResResource = ysr.findById(resId);
        Assert.notNull(elasticYxyResResource, "资源不存在");
        elasticYxyResResource.setPv(elasticYxyResResource.getPv() + 1);
        yrrr.save(elasticYxyResResource);
        // ### 组装model
        RespModel model = new RespModel();
        model.id = elasticYxyResResource.getId();
        model.packName = lecturePack.getPackName();
        String url = "";
        if (elasticYxyResResource.getTypeId().equals("yx14") || elasticYxyResResource.getFormatId().equals("FMT04")) {
            url = builder.setResourceId(resId).setUserId(profile.getId())
                    .setUserVip(Boolean.valueOf(profile.getUserVip())).setResVip(Boolean.valueOf(profile.getResVip()))
                    .setViewFrom("weikecheng").build();
        } else {
            url = yxyResUtil.getPreviewUrl(resId);
        }
        model.dateFormat = DateToolUtils.format(lecturePack.getPreDate(), DateToolUtils.FORMAT_SHORT_YMD);
        model.location = elasticYxyResResource.getLocation();
        model.formatId = elasticYxyResResource.getFormatId();
        model.preview = url;
        model.downloadLocation = yxyResUtil.getDownloadUrl(model.id);
        String extension = elasticYxyResResource.getExtension();
        String examine = url;
        model.examine = examine;
        return model;
    }

    protected static class RespModel extends YxyApiRespModel {
        public String id;
        public String packName;
        public String dateFormat;
        public String location;
        public String formatId;
        public String preview;
        public String downloadLocation;
        public String examine;
    }
}
